﻿using System;
using System.IO;

class Configuration
{
   

    private const string inHTMLNameString       = "INPUT_HTML_FILE_NAME";
    private const string outHTMLPrefixString    = "OUTPUT_HTML_FILE_NAME_PREFIX";
    private const string outAnswersNameString   = "ANSWERS_HTML_OUTPUT_FILE_NAME";
    private const string numTestsString         = "NUMBER_OF_TESTS_TO_GENERATE";
    private const string maxQuestionsString     = "MAX_QUESTIONS_COUNT_IN_TEST";
    private const string maxAnswersString       = "MAX_ANSWERS_PER_QUESTION";

    private string inputHTMLFileName;
    private string outputHTMLFileNamePrefix;
    private string answersHTMLOutputFileName;
    private int numberTestsToGenerate;
    private int maxQuestionsCountTest;
    private int maxAnswersPerQuestion;

    public string INPUT_HTML_FILE_NAME
    {
        get { return inputHTMLFileName; }
    }
    public string OUTPUT_HTML_FILE_NAME_PREFIX
    {
        get { return outputHTMLFileNamePrefix; }
    }
    public string ANSWERS_HTML_OUTPUT_FILE_NAME
    {
        get { return answersHTMLOutputFileName; }
    }
    public int NUMBER_OF_TESTS_TO_GENERATE
    {
        get { return numberTestsToGenerate; }
    }
    public int MAX_QUESTIONS_COUNT_IN_TEST
    {
        get { return maxQuestionsCountTest; }
    }
    public int MAX_ANSWERS_PER_QUESTION
    {
        get { return maxAnswersPerQuestion; }
    }

    private bool TestConfigProperties()
    {
        if (inputHTMLFileName == null || inputHTMLFileName == "") 
            return false;
        if (outputHTMLFileNamePrefix == null || outputHTMLFileNamePrefix == "") 
            return false;
        if (answersHTMLOutputFileName == null || answersHTMLOutputFileName == "")
            return false;

        if (numberTestsToGenerate == 0) 
            return false;
        if (maxQuestionsCountTest == 0) 
            return false;
        if (maxAnswersPerQuestion == 0)
            return false;

        //Console.WriteLine("{0} -> {1}", inHTMLNameString, InputHTMLFileName);
        //Console.WriteLine("{0} -> {1}", outHTMLPrefixString, outputHTMLFileNamePrefix);
        //Console.WriteLine("{0} -> {1}", outAnswersNameString, answersHTMLOutputFileName);
        //Console.WriteLine("{0} -> {1}", numTestsString, numberTestsToGenerate);
        //Console.WriteLine("{0} -> {1}", maxQuestionsString, maxQuestionsCountTest);
        //Console.WriteLine("{0} -> {1}", maxAnswersString, maxAnswersPerQuestion);
        
        return true;
    }

    public Configuration(string initFile)
    {
        try
        {
            StreamReader sr = new StreamReader(initFile);

            string line = sr.ReadLine();
            string[] lineArgs;

            while (line != null)
            {
                lineArgs = line.Split('=');

                if (line.Contains(inHTMLNameString))
                    inputHTMLFileName = lineArgs[1].Trim();

                if (line.Contains(outHTMLPrefixString))
                    outputHTMLFileNamePrefix = lineArgs[1].Trim();

                if (line.Contains(outAnswersNameString))
                    answersHTMLOutputFileName = lineArgs[1].Trim();

                if (line.Contains(numTestsString))
                    numberTestsToGenerate = Int32.Parse(lineArgs[1].Trim());

                if (line.Contains(maxQuestionsString))
                    maxQuestionsCountTest = Int32.Parse(lineArgs[1].Trim());

                if (line.Contains(maxAnswersString))
                    maxAnswersPerQuestion = Int32.Parse(lineArgs[1].Trim());

                line = sr.ReadLine();
            }

            sr.Close();

            if (!this.TestConfigProperties())
            {
                throw new ArgumentException("The configuration file contains invalid data.");
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Configuration file not found.");
        }
    }
 
}